package com.qingzhuge.common.utils;


import lombok.extern.slf4j.Slf4j;
import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.HanyuPinyinVCharType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;

/**
 * @author : zero.xiao
 * @description : 汉字辅助类
 * @modified :
 */
@Slf4j
public final class PinyinUtil {
    private PinyinUtil() {
    }
    /**
     * 将汉字转换为全拼
     *
     * @param src String
     * @return String
     */
    public static String getPinYin(String src) {
        if (src == null) {
            return null;
        }
        char[] chars = src.toCharArray();
        String[] py;
        // 设置汉字拼音输出的格式
        HanyuPinyinOutputFormat pinyinOutputFormat = getHanyuPinyinOutputFormat();
        StringBuilder builder = new StringBuilder();
        try {
            for (char aT1 : chars) {
                // 判断是否为汉字字符
                if (Character.toString(aT1).matches("[\\u4E00-\\u9FA5]+")) {
                    // 将汉字的几种全拼都存到t2数组中
                    py = PinyinHelper.toHanyuPinyinStringArray(aT1, pinyinOutputFormat);
                    // 取出该汉字全拼的第一种读音并连接到字符串t4后
                    builder.append(py[0]);
                } else {
                    // 如果不是汉字字符，直接取出字符并连接到字符串t4后
                    builder.append(aT1);
                }
            }
        } catch (BadHanyuPinyinOutputFormatCombination e) {
            log.error("", e);
        }
        return builder.toString();
    }

    /**
     * 将汉字转换为驼峰全拼
     *
     * @param src String
     * @return String
     */
    public static String getCamelPinYin(String src) {
        char[] chars = src.toCharArray();
        String[] py;
        HanyuPinyinOutputFormat pinyinOutputFormat = getHanyuPinyinOutputFormat();
        StringBuilder builder = new StringBuilder();
        try {
            for (char aT1 : chars) {
                String t;
                // 判断是否为汉字字符
                if (Character.toString(aT1).matches("[\\u4E00-\\u9FA5]+")) {
                    // 将汉字的几种全拼都存到t2数组中
                    py = PinyinHelper.toHanyuPinyinStringArray(aT1, pinyinOutputFormat);
                    // 取出该汉字全拼的第一种读音并连接到字符串t4后
                    t = py[0];
                } else {
                    // 如果不是汉字字符，直接取出字符并连接到字符串t4后
                    t = Character.toString(aT1);
                }
                t = t.substring(0, 1).toUpperCase() + t.substring(1);
                builder.append(t);
            }
        } catch (BadHanyuPinyinOutputFormatCombination e) {
            log.error("", e);
        }
        return builder.toString();
    }

    private static HanyuPinyinOutputFormat getHanyuPinyinOutputFormat() {
        // 设置汉字拼音输出的格式
        HanyuPinyinOutputFormat pinyinOutputFormat = new HanyuPinyinOutputFormat();
        pinyinOutputFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);
        pinyinOutputFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
        pinyinOutputFormat.setVCharType(HanyuPinyinVCharType.WITH_V);
        return pinyinOutputFormat;
    }

    /**
     * 提取每个汉字的首字母
     *
     * @param str String
     * @return String
     */
    public static String getPinYinHeadChar(String str) {
        StringBuilder convert = new StringBuilder();
        for (int j = 0; j < str.length(); j++) {
            char word = str.charAt(j);
            // 提取汉字的首字母
            String[] pinyinArray = PinyinHelper.toHanyuPinyinStringArray(word);
            if (pinyinArray != null) {
                convert.append(pinyinArray[0].charAt(0));
            } else {
                convert.append(word);
            }
        }
        return convert.toString();
    }

    /**
     * 提取每个汉字的大写首字母
     *
     * @param str String
     * @return String
     */
    public static String getPinYinHeadUperChar(String str) {
        return getPinYinHeadChar(str).toUpperCase();
    }
}
